package com.ano.controller;

import com.ano.entity.Cluster;
import com.ano.service.ClusterService;
import com.ano.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("test")
public class TestController {

    @Autowired
    ClusterService clusterService;

    @RequestMapping("test1")
    public String test(String str1, HttpServletRequest req){
        System.out.println("进入test!!!");
        req.setAttribute("str2",str1);
        return "/test/test2";
    }

    @RequestMapping("test2")
    @ResponseBody
    public String test2(String str2,HttpServletRequest req){
        str2 = (String) req.getAttribute("str2");
        System.out.println("进入test2");
        return str2;
    }

    @PostMapping("findByPage")
    @ResponseBody
    public PageInfo<Cluster> PageTest1(@RequestParam(value = "num",defaultValue = "1") Integer num,@RequestParam(value = "size",defaultValue = "9")Integer size){
        return clusterService.findAllForPage(num,size);
    }


    @PostMapping("findByName")
    @ResponseBody
    public PageInfo<Cluster> PageTest2(@RequestParam(value = "num",defaultValue = "1") Integer num,
                                       @RequestParam(value = "size",defaultValue = "9")Integer size,
                                       @RequestParam(value = "clustername") String clustername){
        return clusterService.findByNameForPage(num, size, clustername);
    }

    @PostMapping("findByType")
    @ResponseBody
    public PageInfo<Cluster> PageTest3(@RequestParam(value = "num",defaultValue = "1") Integer num,
                                       @RequestParam(value = "size",defaultValue = "9")Integer size,
                                       @RequestParam(value = "typename") String typename){
        //System.out.println(typename);
        return clusterService.findByTypeForPage(num, size, typename);
    }

    @PostMapping("findByNameAndType")
    @ResponseBody
    public PageInfo<Cluster> PageTest4(@RequestParam(value = "num",defaultValue = "1") Integer num,
                                       @RequestParam(value = "size",defaultValue = "9")Integer size,
                                       @RequestParam(value = "clustername") String clustername,
                                       @RequestParam(value = "typename") String typename){
        //System.out.println(typename);
        return clusterService.findByNameAndTypeForPage(num, size, clustername, typename);
    }

    @PostMapping("findByPage2")
    @ResponseBody
    public ResultUtil<PageInfo<Cluster>> PageTest5(@RequestParam(value = "num",defaultValue = "1") Integer num, @RequestParam(value = "size",defaultValue = "9")Integer size){
        ResultUtil<PageInfo<Cluster>> res = new ResultUtil<PageInfo<Cluster>>();
        PageInfo<Cluster> cluInfo = clusterService.findAllForPage(num, size);
        res.setObj(cluInfo).setMsg("查询成功！！！");
        return res;
    }

}
